<?php

namespace app\admin\model;

use think\Model;
use app\common\model\Region;

class Goods extends Model
{
    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'category_id_text',
        'type_text',
        'status_text',
        'province_text',
        'city_text',
        'area_text',
    ];


    protected static function init()
    {
        self::afterUpdate(function ($row) {
            if ($row['status'] == 1) {
                self::sendWxMsg($row);

            }
        });
    }

    public  static function sendWxMsg($goods)
    {
        $wechat = new \wechat\Wechat();
        $userIdArr = \think\Db::name('message_type')->where('type_id', $goods['type'])->column('user_id');

        $UserIds = \think\Db::name('user')
            ->where(function ($query) {
                $query->where(['is_receive' => '1', 'is_settime' => '0']);
            })
            ->whereOr(function ($query) {
                $query->where([
                    'is_receive' => '1',
                    'is_settime' => '1',
                    'start_time' => ['<', date('H:i')],
                    'end_time' => ['>', date('H:i')]
                ]);
            })
            ->column('id');

        $sendUserIds = array_intersect($userIdArr, $UserIds);
        foreach ($sendUserIds as $k => $v) {
            $wechat->sendWxMsg($v, $goods['id']);
        }
    }

    public function getCategoryIdList()
    {

        $categoryM = new \app\common\model\Category();
        $list = $categoryM->getList('default');
        return $list;
    }


    public function getTypeList()
    {
        $model = new \app\common\model\Type();
        $list = $model->getList();

        return $list;
    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getCategoryIdTextAttr($value, $data)
    {
        $value = $value ? $value : $data['category_id'];
        $list = $this->getCategoryIdList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getProvinceTextAttr($value, $data)
    {
        $value = $value ? $value : $data['province'];
        $model = new Region();
        $list = $model->getList(1);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCityTextAttr($value, $data)
    {
        $value = $value ? $value : $data['city'];
        $model = new Region();
        $list = $model->getList(2);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAreaTextAttr($value, $data)
    {
        $value = $value ? $value : $data['area'];
        $model = new Region();
        $list = $model->getList(3);
        return isset($list[$value]) ? $list[$value] : '';
    }


}
